import requests
from lxml import etree

# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'
# }
# r = requests.get('https://www.douban.com', headers=headers)
# print(r.content.decode())


# 构建参数
# kw = {
#     'wd': 'Java'
# }
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'
# }
# r = requests.get('https://www.baidu.com/s', headers=headers, params=kw)
# print(r.text)


# lxml 模块
# text = '''
#     <div class="app">
#         <div class="container">
#             <div class="row">
#                 <div class="col-md-12">
#                 数据1
#                 </div>
#                 <div class="col-md-12">
#                 数据2
#                 </div>
#                 <div class="col-md-12">
#                 数据3
#                 </div>
#                 <div class="col-md-12">
#                 数据4
#                 </div>
#             </div>
#             <div class="row">
#                 <div class="col-md-12">
#                 数据1
#                 </div>
#                 <div class="col-md-12">
#                 数据2
#                 </div>
#                 <div class="col-md-12">
#                 数据3
#                 </div>
#                 <div class="col-md-12">
#                 数据4
#                 </div>
#             </div>
#         </div>
#     </div>
#             '''
# html = etree.HTML(text)
# # print(html)
# # print(etree.tostring(html))
# result = html.xpath('//div[@class="col-md-12"]')
# print(result)


text = """
<div>
    <ul>
    <li class="item-0"><a href="link1.html">链接1</a></li>
    <li class="item-2 active"><a href="link2.html">item 链接2</a></li>
    <li class="item-3"><a href="link3.html">链接3</a></li>
    <li class="item-4"><a href="link4.html">item 链接4</a>内容介绍：拒收到付老实交代速度快说法</li>
    <li class="item5"><a href="link4.html">链接4</a>内容介绍：拒收到付老实交代速度快说法</li>
    </ul>
</div>
"""
html = etree.HTML(text)
# print(html)
# 获取文本
# result = html.xpath("//li/a/text()")
# print(result)
# result = html.xpath("//li/text()")
# print(result)
# 条件匹配
# result = html.xpath('//li/a[text()="链接2"]')
# print(result)
# 包含
# result = html.xpath('//li[contains(@class,"item-")]')
# print(result)
result = html.xpath('//li/a[contains(text(),"item")]')
print(result)
